package com.tjbank.cssys.web.controller;

import com.tjbank.cssys.domain.CkLevel;
import com.tjbank.cssys.domain.CkLog;
import com.tjbank.cssys.web.service.CkLevelService;
import com.tjbank.cssys.web.service.CkLogService;
import com.tjbank.cssys.web.util.CommonResult;
import com.tjbank.cssys.web.util.GetUserCodeFromToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;


@Api(tags = "CkLevelController", description = "参数配置")
@RestController
@RequestMapping("/level")
public class CkLevelController {
    @Autowired
    private CkLevelService ckLevelService;

    private static final Logger logger = LoggerFactory.getLogger(CkLevelController.class);

    @Autowired
    private CkLogService ckLogService;


    @ApiOperation("获取参数配置查询管理")
    @RequestMapping("/list")
    public CommonResult list(HttpServletRequest request){
        List<CkLevel> ckLevels = ckLevelService.SelectList();
        String userCode=null;
        try {
            userCode= GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取当前登录人信息失败"+e.getMessage());

        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setUserId(userCode);
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setLogName(UUID.randomUUID().toString());
        ckLog.setParams("");
        ckLog.setOperationType("查询风险等级");
        if(CollectionUtils.isNotEmpty(ckLevels)){
            ckLog.setResult("success:200");
            ckLogService.addInsert(ckLog);
            return  CommonResult.success(ckLevels);
        }else{
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return  CommonResult.failed("错误");
        }
    }

    /**/
    @ApiOperation("获取参数配置查询管理")
    @RequestMapping("/listCategory")
    public CommonResult listCategory(@RequestParam(defaultValue = "1") int category,HttpServletRequest request){

        List<CkLevel> ckLevelsCategory = ckLevelService.SelectCategory(category);
        String userCode=null;
        try {
            userCode= GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取登录人信息失败"+e.getMessage());
        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setLogName(UUID.randomUUID().toString());
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setUserId(userCode);
        ckLog.setParams(category+"");
        ckLog.setOperationType("获取参数配置查询管理");
        if(CollectionUtils.isNotEmpty(ckLevelsCategory)){
            ckLog.setResult("success:200");
            ckLogService.addInsert(ckLog);
            return  CommonResult.success(ckLevelsCategory);
        }else{
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return  CommonResult.failed("错误");
        }
    }

   /* @ApiOperation("添加M等级 ")
    @PostMapping("/addM")
    public CommonResult insertM(@RequestBody CkLevel ckLevel){

        boolean b = ckLevelService.insertM(ckLevel);
        return  CommonResult.success(b);
    }*/

    @ApiOperation("添加或更新 ")
    @PostMapping("/add/update")
    public CommonResult insertOrUpdate(@RequestBody CkLevel info , HttpServletRequest request){
        boolean b = ckLevelService.insertOrUpdate(info);
        String userCode=null;
        try {
            userCode= GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取登录人信息失败"+e.getMessage());

        }
        CkLog ckLog = new CkLog();
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setOperationType("添加或更新参数配置");
        ckLog.setParams(ckLog+"");
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setLogName(UUID.randomUUID().toString());
        ckLog.setUserId(userCode);
        if(b){
            ckLog.setResult("success:200");
            ckLogService.addInsert(ckLog);
            return  CommonResult.success(null,"操作成功！");
        }else{
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return  CommonResult.failed("操作失败！");
        }
    }

}
